<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>告警记录</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

	function formatDatebox(value) {
	      if(value ==''||value ==null){
	          return;
		  }else {
              var datetimeType = "";
              var date = new Date(value);
              datetimeType = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
                  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
              return datetimeType;
          }
	}
	
	function formatJsonObject(value) {
		return JSON.stringify(value);
	}
	
	function formatAlarmDetails(val, row, index) {
		var alarmDetails = row.alarmDetails;
		if(row.alarmType==1){
			return "服务器号:"+alarmDetails.serverId+
					" , CPU使用率:"+alarmDetails.cpuUsageRate+
					"% , CPU使用率告警门限:"+alarmDetails.cpuUsageThreshold+"%";
		}else if(row.alarmType==2){
			return "服务器号:"+alarmDetails.serverId+
					" , 内存使用率:"+alarmDetails.memoryUsageRate+
					"% , 内存使用率告警门限:"+alarmDetails.memoryUsageThreshold+"%";
		}else if(row.alarmType==3){
			return "服务器号:"+alarmDetails.serverId+
					" , 硬盘使用率:"+alarmDetails.harddiskUsageRate+
					"% , 硬盘使用率告警门限:"+alarmDetails.harddiskUsageThreshold+"%";
		}else if(row.alarmType==4){
                return "前置机号:" + alarmDetails.frontProcessorId +
                    " , 前置机故障信息:" + alarmDetails.errCode
                /* +
                " , 故障时间:"+formatDatebox(alarmDetails.timestamp*1000) */
							;
		}else if(row.alarmType==5){
            return ""
                /* +
                " , 故障时间:"+formatDatebox(alarmDetails.timestamp*1000) */
                ;
        }else if(row.alarmType==15){
            return "前置机号:" + alarmDetails.frontProcessorId +
                " , 前置机故障信息:" + "上行无数据告警"
			/*+"前置机号:"+alarmDetails.frontProcessorId
			 +
					" , 掉电重启时间:"+formatDatebox(alarmDetails.timestamp*1000) */
					;
		}else if(row.alarmType==6){
			return "服务器号:"+alarmDetails.serverId+
					" , 丢包率:"+alarmDetails.packetLossRate+
					"% , 丢包告警门限:"+alarmDetails.packetLossRateThreshold+"%";
		}else if(row.alarmType==7){
			return "CPU内存使用率:"+alarmDetails.cpuUsageRate+
			"% , CPU使用率告警门限:"+alarmDetails.cpuUsageThreshold+"%";
		}else if(row.alarmType==8){
			return "内存使用率:"+alarmDetails.memoryUsageRate+
			"% , 内存使用率告警门限:"+alarmDetails.memoryUsageThreshold+"%";
		}else if(row.alarmType==9){
			return  "硬盘使用率:"+alarmDetails.harddiskUsageRate+
			"% , 硬盘使用率告警门限:"+alarmDetails.harddiskUsageThreshold+"%";
		}else if(row.alarmType==10){
			return  "前置机网络连接告警";
		}else{
			return formatJsonObject(row.alarmDetails);
		}
		
	}
	function formatAlarmClass(val, row, index) {
		var alarmType = row.alarmType;
		if (alarmType == 1 || alarmType == 2 ||alarmType == 3 ||alarmType == 7 ||alarmType == 8 ||alarmType == 9) {
			return "一般告警";
		}
		return "紧急告警";
	}
	
	function formatAlarmType(value) {
		if(value==1){
			return "服务器CPU使用率告警";
		}else if(value==2){
			return "服务器内存使用率告警";
		}else if(value==3){
			return "服务器硬盘使用率告警";
		}else if(value==4){
			return "前置机故障";
		}else if(value==5){
			return "前置机掉电重启";
		}else if(value==7){
			return "前置机CPU使用率告警";
		}else if(value==8){
			return "前置机内存使用率告警";
		}else if(value==9){
			return "前置机硬盘使用率告警";
		}else if(value==10){
			return "前置机网络连接告警";
		}else if (value == 13) {
			return "外网强信号风险";
		}else if (value == 14) {
			return "外网翻频风险";
		}else if(value == 15){
			return "上行无数据告警";
		}else if(value == 20){
            return "频谱数据入库异常告警";
        }else if(value == 21){
            return "电路域数据入库异常告警";
        }
		else {
		    return value;
		}
	}
	
	function markHandled() {
		var selectedRows=$("#dg").datagrid("getSelections");
		 if(selectedRows.length==0){
			 $.messager.alert("系统提示","请选择要标记的数据！");
			 return;
		 }
		 var strIds=[];
		 var alarmType=[];
		 for(var i=0;i<selectedRows.length;i++){
			 strIds.push(selectedRows[i].alarmInfoId);
			 alarmType.push(selectedRows[i].alarmType);
		 }
		 var alarmInfoIds=strIds.join(",");
		 var alarmTypes=alarmType.join(",");
		 console.log(alarmTypes);
		 var isHandle=1;
		 $.messager.confirm("系统提示","您确定要标记这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
				if(r){
					$.post("${pageContext.request.contextPath}/alarmInfo/save.do",{alarmInfoIds:alarmInfoIds,alarmTypes:alarmTypes,isHandle:isHandle,flag:2,comfirmer:'${currentMemberShip.user.id}'},function(result){
						if(result.success){
							 $.messager.alert("系统提示","已标记成功！");
							 $("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","标记失败，请联系系统管理员！");
						}
					},"json");
				} 
			 });
	}
	
	function markUnHandled() {
		var selectedRows=$("#dg").datagrid("getSelections");
		 if(selectedRows.length==0){
			 $.messager.alert("系统提示","请选择要标记的数据！");
			 return;
		 }
		 var strIds=[];
		 for(var i=0;i<selectedRows.length;i++){
			 strIds.push(selectedRows[i].alarmInfoId);
		 }
		 var alarmInfoIds=strIds.join(",");
		 var isHandle=0;
		 $.messager.confirm("系统提示","您确定要标记这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
				if(r){
					$.post("${pageContext.request.contextPath}/alarmInfo/save.do",{alarmInfoIds:alarmInfoIds,alarmTypeIds:alarmType,isHandle:isHandle,flag:2},function(result){
						if(result.success){
							 $.messager.alert("系统提示","已标记成功！");
							 $("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","标记失败，请联系系统管理员！");
						}
					},"json");
				} 
			 });
	}
	
	function formatIsHandle(value) {
		if(value==0){
			return "<font color=red>否</font>";
		}else if(value==1){
			return "是";
		}else{
			return value;
		}
	}
	
	function formatIsNormal(value) {
		
		if (value == 1) {
			return '自动解除';
		} else if (value == 2) {
			return '手动解除';
		}
		return '';
	}
	
	function formatFrontProcessorId(val, row, index) {
		var alarmDetails = row.alarmDetails;
		return alarmDetails.frontProcessorId;
	}
	
	function searchAlarmInfo(){
		
		var level = $("#alarmType1").combotree("getValues");
		console.log(level.toString());
		var t = [];
		for (var i = 0; i < level.length; i++) {
			t.push(level[i]);
		}
		console.log(t);

		var ids = t.join("x");
		console.log(ids);

		var handle = $("#handle").combotree("getText");
		console.log(handle);
		if(handle == '未确认未清除') handle = '0';
		else if(handle == '已确认未清除') handle = '1';
        else if(handle == '未确认已清除') handle = '2';
        else if(handle == '已确认未清除,未确认已清除') handle = '3';
        else if(handle == '未确认未清除,未确认已清除') handle = '4';
        else if(handle == '未确认未清除,已确认未清除') handle = '5';
        else if(handle == '全选,未确认未清除,已确认未清除,未确认已清除') handle = '6';
		else handle = '7';
		
		var frontProcessorId = $("#frontProcessorId").val()
		 $("#dg").datagrid('load',{
			"siteName":$("#siteName").combotree("getText"),
			"frontProcessorId":frontProcessorId,
			"alarmTypeIds": level.toString(),
			"isHandle" : handle
		 });
	}
	
	function exportAlarmInfoInCurrentPage(){
		var rows = $("#dg").datagrid("getRows");
		 var strIds=[];
		 for(var i=0;i<rows.length;i++){
			 strIds.push(rows[i].alarmInfoId);
		 }
		 var ids=strIds.join(",");
		 window.open('${pageContext.request.contextPath}/alarmInfo/export.do?alarmInfoIds='+ids+'&realtime=1');
	}
	function exportByTemplateInCurrentPage(){
		var rows = $("#dg").datagrid("getRows");
		var strIds=[];
		 for(var i=0;i<rows.length;i++){
			 strIds.push(rows[i].alarmInfoId);
		 }
		 var ids=strIds.join(",");
		 window.open('${pageContext.request.contextPath}/alarmInfo/exportByTemplate.do?alarmInfoIds='+ids+'');
	}
	
	function exportAlarmInfoInAllPages(){
		var frontId=$("#siteName").combotree("getText");
		var alarmType=$("#alarmType1").combotree("getValues");
		var isHandle=$("#handle").combotree("getText");
		console.log("ishandle:"+isHandle);
		window.open('${pageContext.request.contextPath}/alarmInfo/exportQuery.do?frontId='+frontId+'&alarmTypeIds='+alarmType.toString()
				+'&isHandle='+isHandle+'&realtime='+1);
	}
	
	function exportByTemplateInAllPages(){
		 window.open('${pageContext.request.contextPath}/alarmInfo/exportByTemplate.do?alarmInfoIds='+null+'');
	}
	
	function formatAlarmTime(val, row, index) {
		var alarmDetails = row.alarmDetails;
		if(!alarmDetails.timestamp){
			
			return formatDatebox(val);
		}else{
			//if(row.alarmType == 10){
				//return formatDatebox(alarmDetails.timestamp);
			//}
			return formatDatebox(val);
		}
	}
	$(function(){		
		$('#handle').combotree('loadData', [
	    	{id:'1000', text:'全选', "state":"open", children: [
                    {id: 1, text: '未确认未清除'},
                    {id: 2, text: '已确认未清除'},
                    {id: 3, text: '未确认已清除'}
		    ]}
		]);
		$('#handle').combotree('setValue', 1000);
		
		$('#dg').datagrid({
			rowStyler:function(index,row){
				// var alarmType = row.alarmType;
				var isHandle =row.isHandle;
                var isNormal =row.isNormal;
                console.info("isHandle"+isHandle+"============"+"isNormal"+isNormal);
				if (isHandle == 0 && isNormal == 0) {
					return 'background-color:#E75265;';
				}
                if (isHandle == 1 && isNormal == 0) {
                    return 'background-color:#FF8000;';
                }
                if (isHandle == 0 && isNormal == 1) {
                    return 'background-color:#FFFF00;';
                }
			}
		});
		
		$('#alarmType1').combotree('loadData', [
			{id:'-1', text:'一般告警',  children: [
		    	{id: 1, text: '服务器CPU使用率告警'},
		    	{id: 2, text: '服务器内存使用率告警'},
		    	{id: 3, text: '服务器硬盘使用率告警'},
		    	{id: 7, text: '前置机CPU使用率告警'},
		    	{id: 8, text: '前置机内存使用率告警'},
		    	{id: 9, text: '前置机硬盘使用率告警'}]},
		    {id:'-2', text:'紧急告警',  children: [
                    {id: 4, text: '前置机故障或上行无数据告警'},
                    {id: 5, text: '前置机掉电重启'},
                    {id: 10, text: '前置机网络连接告警'},
                    {id: 20, text: '频谱数据入库异常告警'},
                    {id: 21, text: '电路域数据入库异常告警'}
  		    	]}
		]);
		
		$('#alarmType1').combotree('setValues', [1,2,3,4,5,6,7,8,9,10,20,21]);
		
		searchAlarmInfo();
		
	});
	
	
</script>
</head>
<body style="margin: 1px">
<table id="dg" title="实时告警信息" class="easyui-datagrid"
   fitColumns="false" pagination="true" rownumbers="true" pageSize="100" pageList="[100,200,300,400,500]"
   url="${pageContext.request.contextPath}/alarmInfo/listUnhandle.do" fit="true" toolbar="#tb">
   <thead>
   	<tr>
   		<th field="cb" checkbox="true" align="center"></th>
   		<th field="alarmInfoId" width="80" align="center">告警编号</th>
   		<th field="alarmType1" width="100" align="center" formatter="formatAlarmClass">告警级别</th>
   		<th field="alarmObjectType" width="120" align="center">告警对象类型</th>
   		<th field="alarmObjectInfo" width="150" align="center">告警对象标识</th>
   		<!-- <th field="frontProcessorId" width="200" align="center" formatter="formatFrontProcessorId">前置机号</th>
   		<th field="siteName" width="200" align="center" >小区</th> -->
   		<th field="alarmType" width="200" align="center" formatter="formatAlarmType">告警原因</th>
   		<th field="alarmTime" width="200" align="center" formatter="formatAlarmTime">首次发生时间</th>
		<th field="lastTime" width="100" align="center" formatter="formatAlarmTime">最近发生时间</th>
		<th field="count" width="100" align="center">发生次数</th>
		<th field="endTime" width="100" align="center" formatter="formatAlarmTime">清除时间</th>
		<th field="comfirmTime" width="100" align="center" formatter="formatAlarmTime">确认时间</th>
		<th field="comfirmer" width="100" align="center" >确认人</th>
   		<th field="isHandle" hidden="hiden" width="100" align="center" formatter="formatIsHandle">是否已处理</th>
   		<th field="isNormal" hidden="hiden" width="100" align="center" formatter="formatIsNormal">解除方式</th>
   		<th field="alarmDetails" width="500" align="center" formatter="formatAlarmDetails">备注</th>
   	</tr>
   </thead>
 </table>
 
 <div id="tb">
 	<div>
 		<a href="javascript:exportAlarmInfoInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出当前页告警信息</a>
 		<a href="javascript:exportAlarmInfoInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出所有页告警信息</a>
 		<!-- <a href="javascript:exportByTemplateInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出当前告警信息</a> -->
<!--  		<a href="javascript:exportByTemplateInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出所有告警信息</a>--> 	
	</div>
 	<div>
 		&nbsp;&nbsp;&nbsp;告警对象：
 		<input class="easyui-combotree" id="siteName" name="siteName" style="width:180px" data-options="panelHeight:'auto', panelMaxHeight:'240px', url:'${pageContext.request.contextPath}/alarmInfo/frontBaseName.do'" multiple/>    		
 		&nbsp;&nbsp;&nbsp;告警状态：
 		<input class="easyui-combotree" id="handle" name="handle" data-options="panelHeight:'auto',panelMaxHeight:'240px'" style="width:110px" multiple/>
					    		
		<!-- 
		<input type="text" class="easyui-combobox" id="alarmLevel" name="alarmLevel"  data-options="panelHeight:'auto',valueField:'id',textField:'name', data:[
					    {id:'0', name:'一般告警'},
					    {id:'1', name:'紧急告警'}]"  style="width:calc(7%)"/>
					    
		&nbsp;&nbsp;&nbsp;告警类型：		    
		<input type="text" class="easyui-combobox" id="alarmLevel" name="alarmLevel"  data-options="panelHeight:'auto',valueField:'id',textField:'name', data:[
					    {id:'1', name:'服务器CPU使用率告警'},
					    {id:'2', name:'服务器内存使用率告警'},
					    {id:'3', name:'服务器硬盘使用率告警'},
					    {id:'4', name:'前置机故障'},
					    {id:'5', name:'前置机掉电重启'},
					    {id:'6', name:'丢包率告警'},
					    {id:'7', name:'前置机CPU使用率告警'},
					    {id:'8', name:'前置机内存使用率告警'},
					    {id:'9', name:'前置机硬盘使用率告警'},
					    {id:'10', name:'前置机网络连接告警'},
					    {id:'11', name:'GPRS数据中断告警'},
					    {id:'12', name:'GPRS数据CRC校验异常'}]"  style="width:calc(11%)"/>
		 -->	    
		&nbsp;&nbsp;&nbsp;告警原因：
		<select id="alarmType1" class="easyui-combotree" style="width:220px;" multiple></select>
 		<a href="javascript:searchAlarmInfo()" class="easyui-linkbutton" iconCls="icon-search" plain="true"><b style="font-size:15px">搜索</b></a>
 		<c:if test="${currentMemberShip.group.name=='管理员' }">
 		<a href="javascript:markHandled()" class="easyui-linkbutton" iconCls="icon-check" plain="true">标记为已处理</a>
 		<%--<a href="javascript:markUnHandled()" class="easyui-linkbutton" iconCls="icon-check" plain="true">标记为未处理</a>--%>
 		</c:if>
 	</div>
 </div>
	<audio id="mp3" src="http://139.159.219.76:8080/smzy.mp3"></audio>
</body>
<script type="text/javascript">
	<%--setInterval("myInterval()", 10000);--%>
	<%--var lastTotal = 0;--%>
	<%--function myInterval() {--%>

		<%--$.post("${pageContext.request.contextPath}/alarmInfo/getTotal.do", {--%>
			<%--lastTotal : lastTotal--%>
		<%--}, function(result) {--%>
			<%----%>
			<%--if (result.total > 0 && (result.total - lastTotal > 0)--%>
					<%--&& result.rows.length > 0 && lastTotal > 0) {--%>
				 <%--$(function(){--%>
					    <%--function playSound(src) {--%>
					    <%--var auto = $("#auto");--%>
					    <%--auto.attr("src",src);--%>
					    <%--}--%>
				<%--})--%>
				<%--for (var i = 0; i < result.rows.length; i++) {--%>
					<%--var desc = "有新的故障告警信息";--%>
					<%--var type = result.rows[i]["alarmType"];--%>
					<%--if (type == "5") {--%>
						<%--desc = "前置机掉电重启"--%>
					<%--} else if (type == "4") {--%>
						<%--desc = "前置机故障"--%>
					<%--} else if (type == "3") {--%>
						<%--desc = "服务器硬盘使用率告警"--%>
					<%--} else if (type == "2") {--%>
						<%--desc = "服务器内存使用率告警"--%>
					<%--} else if (type == "1") {--%>
						<%--desc = "服务器CPU使用率告警"--%>
					<%--} else if (type == "6") {--%>
						<%--desc = "丢包率告警"--%>
					<%--} else if (type == "7") {--%>
						<%--desc = "前置机CPU使用率告警"--%>
					<%--} else if (type == "8") {--%>
						<%--desc = "前置机内存使用率告警"--%>
					<%--} else if (type == "9") {--%>
						<%--desc = "前置机硬盘使用率告警"--%>
					<%--} else if(type==10) {--%>
						<%--desc =  "前置机网络连接告警";--%>
					<%--}else if(type==11) {--%>
						<%--desc =  "GPRS数据中断告警";--%>
					<%--}else if(type==12) {--%>
						<%--desc =  "GPRS数据CRC校验异常";--%>
					<%--}--%>
					<%----%>
					<%--$.messager.alert("系统提示", desc);--%>
					<%--document.getElementById("mp3").play();--%>
				<%--}--%>
			<%--}--%>
			<%--lastTotal = result.total;--%>
		<%--}, "json");--%>
</script>
</html>